package com.kfm.shop.system.controller;

import com.kfm.shop.common.util.Result;
import com.kfm.shop.system.model.SysMenu;
import com.kfm.shop.system.service.SysMenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 菜单管理 controller
 */
@RestController
@RequestMapping("/sysMenu")
@Api(tags = "菜单管理")
public class SysMenuController {

    @Autowired
    private SysMenuService sysMenuService;

    @GetMapping
    @ApiOperation(value = "获取所有菜单信息", notes = "获取所有菜单信息")
    public Result getGoods(@ApiParam(name = "sysMenu", value = "菜单信息") SysMenu sysMenu) {
//        throw new RuntimeException("模拟异常");
        return Result.ok(sysMenuService.treeList(sysMenu));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "根据id获取菜单信息", notes = "根据id获取菜单信息")
    public Result getGoodsById(@ApiParam(name = "id", value = "菜单id", required = true) @PathVariable Integer id) {
        return Result.ok(sysMenuService.getById(id));
    }


    @PostMapping
    @ApiOperation(value = "根据id修改菜单信息", notes = "根据id修改菜单信息")
    public Result updateGoodsById(@ApiParam(name = "sysMenu", value = "菜单信息", required = true) SysMenu sysMenu) {
        return Result.ok(sysMenuService.updateById(sysMenu));
    }


    @PutMapping
    @ApiOperation(value = "新增菜单信息", notes = "新增菜单信息")
    public Result addGoods(@ApiParam(name = "sysMenu", value = "菜单信息", required = true) SysMenu sysMenu) {
        return Result.ok(sysMenuService.save(sysMenu));
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除菜单信息", notes = "根据id删除菜单信息")
    public Result deleteGoodsById(@ApiParam(name = "id", value = "菜单id", required = true) @PathVariable Integer id) {
        return Result.ok(sysMenuService.removeById(id));
    }

}
